<?php

class User_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Update user profile
     * @param type $fields
     * @param type $conditions
     * @return boolean 
     */
    public function update($fields = array(), $conditions = array())
    {
        if (empty ($fields) || empty ($conditions)){
            return false;
        }
        
        $this->db->update('users', $fields, $conditions);
        
        //@TODO: clear cache;
        
        return $this->db->affected_rows();
    }
    
    /**
     * Get user by id
     * @param int $user_id
     * @return array 
     */
    public function get_by_id($user_id)
    {
        //@TODO: apply cache here
        return $this->db->query("SELECT * FROM users WHERE userid = ?", array($user_id))->row_array();
    }
    
    /**
     * Check email is exist
     * @param type $email
     * @return boolean 
     */
    public function is_email_exist($email)
    {
        $query = $this->db->query("SELECT * FROM users WHERE email = ?", array($email));
        
        return ($query->num_rows() > 0) ? false : true;
    }
    
    /**
     * Insert new user
     * @param type $arrValues
     * @return int 
     */
    public function insert($arrValues)
    {
        $this->db->insert('users', $arrValues);
        
        //@TODO: write cache here
        
        return $this->db->insert_id();
    }

}

